#!/bin/bash
echo "开始退出程序"

pidfileName=sm.pid
if [ -f "${pidfileName}" ]; then
    pid=$(cat ${pidfileName})
    kill -2 ${pid}

    count=0
    while true
    do
			PID_EXIST=$(ps aux | awk '{print $2}'| grep -w $pid)

			if [ ! $PID_EXIST ];then
                break;
            else
                sleep 1;
                count=$[count+1]
                if [ $count -gt 20 ];then
                    echo "等待超时，强制退出! ..."
					kill -7 ${pid}
                    exit 1
                fi

                echo "等待进程退出："${count};
            fi

    done

    echo "删除pid文件："${pidfileName}
    rm ${pidfileName}
else
    echo "pid文件：${pidfileName} 不存在"
fi